package com.hnkywl.sts.service.kcgl;

import com.hnkywl.sts.dao.cggl.CgdmxDao;
import com.hnkywl.sts.dao.htgl.CghtDao;
import com.hnkywl.sts.dao.kcgl.SjdjDao;
import com.hnkywl.sts.dao.kcgl.SskcDao;
import com.hnkywl.sts.dao.xsgl.XsmxDao;
import com.hnkywl.sts.entity.cggl.Cgdmx;
import com.hnkywl.sts.entity.sys.Gg;
import com.hnkywl.sts.entity.xsgl.Kh;
import com.hnkywl.sts.entity.xsgl.Sjdj;
import com.hnkywl.sts.entity.xsgl.Xsmx;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-25
 * Time: 下午2:39
 * To change this template use File | Settings | File Templates.
 */
@Service
public class SjdjService {
    private XsmxDao xsmxDao;
    private SskcDao sskcDao;
    private SjdjDao sjdjDao;
    private CghtDao cghtDao;
    private CgdmxDao cgdmxDao;

    @Transactional
    public String findAllSjdjList(String carno, String fhrq, String xsdh, String htbm, Integer zt, int pageNo, int pageSize) {
        Page<Sjdj> page = this.sjdjDao.findSjdjList(carno, fhrq, xsdh, htbm, zt, pageNo, pageSize);
        List<Sjdj> list = page.getResult() == null ? new ArrayList<Sjdj>() : page.getResult();
        String json = BaseUtils.fetchCollectionToJson(list, "id", "htbm", "qdrq", "gname", "gcz", "ggxh", "gdw", "xsdh", "carno", "fhrq", "zzl", "sjdj", "zt", "cxm", "crq", "shr", "shrq");
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":[" + json + "]}";
    }


    /**
     * 判断导入的数据是否存在
     *
     * @param xsdh
     * @param carNo
     * @param fhrq
     * @param jszl
     * @return
     */
    public boolean checkData(String xsdh, String carNo, String fhrq, String jszl) {
        List<Map> list = this.sskcDao.findSjdjList(xsdh, carNo, fhrq);
        Double zzl = Double.parseDouble(jszl);
        if (null != list && list.size() > 0) {
            Double rksl = Double.parseDouble(list.get(0).get("RKSL").toString());
            if (zzl.equals(rksl)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 保存实际单价
     *
     * @param listMap
     */
    @Transactional
    public void saveSjdj(List<Map<String, String>> listMap) {
        for (Map map : listMap) {
            Sjdj sjdj = new Sjdj();
            List<Map> list = this.xsmxDao.findCght(map.get("xsdh").toString());
            if (list != null && list.size() > 0) {
                sjdj.setCght(this.cghtDao.get(Long.valueOf(list.get(0).get("ID").toString())));
            } else {
                throw new RuntimeException("没有查询到对应的采购合同!");
            }

            //检查实际单价表中是否已存在该条物资的后定价
            List<Map> list_sjdj = this.sjdjDao.findSjdjList_sjdj((String)map.get("xsdh"),(String)map.get("carNo"),(String) map.get("fhrq"));
            //如果存在，则将当前单价修改到之前的后定价单价
            if(list_sjdj != null && list_sjdj.size() > 0){
                    this.sjdjDao.updateSjdj((String)map.get("xsdh"),(String)map.get("carNo"),(String) map.get("fhrq"),Double.parseDouble(map.get("sjdj").toString()));
            }
            //如果不存在,则新增一条后定价记录
            else {
                sjdj.setXsdh(map.get("xsdh").toString());
                Gg gg = this.sskcDao.findJtwz(map.get("xsdh").toString());
                sjdj.setJtwz(gg);
                sjdj.setCarno(map.get("carNo").toString());
                sjdj.setFhrq(map.get("fhrq").toString());
                sjdj.setZzl(Double.parseDouble(map.get("jszl").toString()));
                sjdj.setSjdj(Double.parseDouble(map.get("sjdj").toString()));
                sjdj.setZt(0);
                sjdj.setDateCreated(new Date());
                sjdj.setCreateOrgan(YgService.getCurrentYg().getZggs());
                sjdj.setCreateDepartment(YgService.getCurrentYg().getCreateDepartment());
                sjdj.setCreateUser(YgService.getCurrentYg());
                this.sjdjDao.save(sjdj);
            }
        }
    }

    /**
     * 保存审核结果
     *
     * @param sjdjIds
     * @param shzt
     */
    @Transactional
    public void saveSjdjSh(String sjdjIds, String shzt) {
        String[] strId = sjdjIds.split(",");
        for (String sjdjId : strId) {
            if (StringUtils.isNotBlank(sjdjId)) {
                Sjdj sjdj = this.sjdjDao.get(Long.valueOf(sjdjId));
                if (StringUtils.equals("no", shzt)) {
                    sjdj.setZt(2);
                    this.sjdjDao.save(sjdj);
                } else {
                    // 查询要更新的实时库存
                    List<Kh> khList = this.sskcDao.findKhForSjdj(sjdj.getXsdh(), sjdj.getCarno(), sjdj.getFhrq());
                    if (null != khList && khList.size() > 0) {
                        for (Kh kh : khList) {
                            kh.setXsxt_price(sjdj.getSjdj());
                            kh.setSfdrjg("1");
                            this.sskcDao.save(kh);
                            // 查询要更新的采购明细
                            Cgdmx cgdmx = this.cgdmxDao.findCgdmxByBindNo(kh.getBindNo());
                            if (null != cgdmx) {
                                cgdmx.setSjdj(sjdj.getSjdj());
                                this.cgdmxDao.save(cgdmx);
                            }
                            // 查询要更新的销售明细
                            Xsmx xsmx = this.xsmxDao.findXsmxByBindNo(kh.getBindNo());
                            if (null != xsmx) {
                                xsmx.setSjdj(sjdj.getSjdj());
                                this.xsmxDao.save(xsmx);
                            }
                        }
                    }
                    sjdj.setZt(1);
                    this.sjdjDao.save(sjdj);
                }
            }
        }
    }

    @Autowired
    public void setXsmxDao(XsmxDao xsmxDao) {
        this.xsmxDao = xsmxDao;
    }

    @Autowired
    public void setSskcDao(SskcDao sskcDao) {
        this.sskcDao = sskcDao;
    }

    @Autowired
    public void setSjdjDao(SjdjDao sjdjDao) {
        this.sjdjDao = sjdjDao;
    }

    @Autowired
    public void setCghtDao(CghtDao cghtDao) {
        this.cghtDao = cghtDao;
    }

    @Autowired
    public void setCgdmxDao(CgdmxDao cgdmxDao) {
        this.cgdmxDao = cgdmxDao;
    }
}
